/* Copyright (C) 2011 clueless <clueless.mmopeon@gmail.com>
 *
 * This file is part of MMO Peon.
 *
 * MMO Peon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MMO Peon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MMO Peon.  If not, see <http://www.gnu.org/licenses/>.
 */


// TODO: Need a better cleaner way to log stuff

#include <fstream>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include "log.h"

HANDLE logMutex;
std::ofstream logStream;
std::ofstream packetStream;

bool LogInit()
{
    logMutex = CreateMutex(0, false, 0);
    if (!logMutex)
        return false;

    const char *appData = getenv("APPDATA");
    if (!appData)
    {
        CloseHandle(logMutex);
        return false;
    }

    std::string path = appData;
    path += "\\rombot.log";

    logStream.open(path.c_str(), std::ofstream::out | std::ofstream::trunc);
    if (logStream.fail())
    {
        CloseHandle(logMutex);
        return false;
    }

    path = appData;
    path += "\\rompackets.log";
    packetStream.open(path.c_str(), std::ofstream::out | std::ofstream::trunc);
    if (logStream.fail())
    {
        logStream.close();
        CloseHandle(logMutex);
        return false;
    }

    return true;
}

void LogWrite(LogType type, const std::string& data)
{
    WaitForSingleObject(logMutex, INFINITE);
    switch(type)
    {
        case LOG_PACKET:
            packetStream << data << std::endl;
            break;
        case LOG_NORMAL:
        default:
            logStream << data << std::endl;
            break;
    }
    ReleaseMutex(logMutex);
}

void LogFree()
{
    CloseHandle(logMutex);
    logStream.close();
    packetStream.close();
}

std::string HexDump(const uint8_t *buffer, uint32_t len)
{
    std::ostringstream hex;
    std::ostringstream plain;
    std::ostringstream all;

    for (uint32_t i = 0; i < len; i++)
    {
        if (i % 16 == 0)
            hex << std::hex << std::setfill('0') << std::setw(8) << i << "    ";     // Offsets
        else if (i % 4 == 0)
            hex << " ";  // 4 byte marker

        hex << std::hex << std::setw(2) << (uint32_t)buffer[i] << " ";    // Hexbytes

        //plainbytes
        if (buffer[i] >= ' ' && buffer[i] <= '~')
            plain << buffer[i];
        else
            plain << ".";

        // Spacefill
        if (i == (len - 1))
            for (uint32_t j = 0; (i + j + 1) % 16 != 0; j++)
                if ((i + j) % 4 == 0)
                    hex << "    ";
                else
                    hex << "   ";

        // line
        if ((i + 1) % 16 == 0 || i == (len - 1))
        {
            all << hex.str() << "   " << plain.str() << std::endl;
            hex.str("");
            plain.str("");
        }
    }

    return all.str();
}
